/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import team.mai.antexpress.entities.DelivermanEntity;

import java.util.List;

@SuppressWarnings("Duplicates")
@Repository

public class DelivermanDAO {

	@Autowired
	SessionFactory sessionFactory;

	public DelivermanEntity getDelivermanByDelivermanname(String delname) {
		try (Session session = sessionFactory.openSession();) {
			Query query = session.createQuery("FROM DelivermanEntity WHERE name=:name");
			query.setParameter("name", delname);
			List list = query.list();
			return list.size() < 1 ? null : (DelivermanEntity) list.get(0);
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		}
	}

	public DelivermanEntity getDelivermanById(int id){
		try (Session session = sessionFactory.openSession();) {
			return session.get(DelivermanEntity.class, id);
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		}
	}
	public int getDelivermanIdByDelivermanname(String delname){
		try(Session session = sessionFactory.openSession()){
			Query<DelivermanEntity> query = session.createQuery("FROM DelivermanEntity WHERE name=:delname");
			query.setParameter("delname", delname);
			List<DelivermanEntity> list = query.list();
			return list.get(0).getId();
		} catch (Exception e){
			e.printStackTrace();
			return 0;
		}
	}
	public boolean insert(DelivermanEntity delToInsert) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			session.save(delToInsert);
			transaction.commit();
			return true;
		} catch (Exception ex) {
			return false;
		}
	}
	public boolean deleteDeliverman(DelivermanEntity user){
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			session.delete(user);
			transaction.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public boolean deleteDelivermanById(int id) {
		return deleteDeliverman(sessionFactory.openSession().get(DelivermanEntity.class, id));
	}

	public boolean updateDeliverman(DelivermanEntity user){
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			session.update(user);
			transaction.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public List<DelivermanEntity> getAllDelivermans(){
		try (Session session = sessionFactory.openSession()) {
			Query query = session.createQuery("FROM DelivermanEntity ");
			List<DelivermanEntity> list = query.list();
			return list;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
}
